#pragma once

#include "Business/Variety.h"

struct PendingMarket
{
	PendingMarket()
		:contract(0),direction(Direction::BUY),price(0),quantity(0)
	{
	}
	PendingMarket(int i_contract,Direction::TYPE i_direction,int i_price,int i_quantity)
		:contract(i_contract),direction(i_direction),price(i_price),quantity(i_quantity)
	{
	}
	int contract;
	Direction::TYPE direction;
	int price;
	int quantity;
};

class PendingMarkets 
{
public:
	typedef std::map<int,int> PendingMarketMap;
public:

	void Clear()
	{
		m_buys.clear();
		m_sells.clear();
	}

	void Update(PendingMarket const& pendingMarket)
	{
		if (pendingMarket.direction == Direction::BUY)
		{
			Buy(pendingMarket.price,pendingMarket.quantity);
		}
		else
		{
			Sell(pendingMarket.price,pendingMarket.quantity);
		}
	}

	void Buy(int price, int quantity)
	{
		if (quantity == 0)
		{
			m_buys.erase(price);
		}
		else
		{
			m_buys[price] = quantity;
		}
	}

	void Sell(int price, int quantity)
	{
		if (quantity == 0)
		{
			m_sells.erase(price);
		}
		else
		{
			m_sells[price] = quantity;
		}
	}

	void AddBuy(int price, int quantity)
	{
		m_buys[price] += quantity;
	}

	void AddSell(int price, int quantity)
	{
		m_sells[price] += quantity;
	}

	PendingMarkets::PendingMarketMap const& GetBuys() const { return m_buys; }
	PendingMarkets::PendingMarketMap const& GetSells() const { return m_sells; }
private:
	PendingMarketMap m_buys;
	PendingMarketMap m_sells;

	void InternalUpdate(std::map<int,int>& pendings,PendingMarket const &market)
	{
		if (market.quantity == 0)
			pendings.erase(market.price);
		else
			pendings[market.price] = market.quantity;
	}
};